package com.fhminyi.GUI;


import com.fhminyi.model.User;
import com.fhminyi.GUI.ProductManagementFrame;
import com.fhminyi.GUI.UserProfileFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class WelcomeFrame extends JFrame {
    private User user;

    public WelcomeFrame(User user) {
        this.user = user;
        setTitle("欢迎页");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(6, 2)); // 修改布局以适应新增按钮

        JButton viewProductsButton = new JButton("浏览商品列表");
        JButton productManagementButton = new JButton("商品管理");
        JButton placeOrderButton = new JButton("查看订单");
        JButton settingsButton = new JButton("设置个人信息");
        JButton manageButton = new JButton("后台管理"); // 管理员按钮

        panel.add(viewProductsButton);
        panel.add(productManagementButton);
        panel.add(placeOrderButton);
        panel.add(settingsButton);
        if (user.getRole().equals("管理员")) {
            panel.add(manageButton);
        } else {
            panel.add(new JLabel()); // 空标签占位
        }

        viewProductsButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new ProductListFrame(user).setVisible(true);
            }
        });

        settingsButton.addActionListener(new ActionListener() { // 添加设置个人信息按钮事件监听器
            @Override
            public void actionPerformed(ActionEvent e) {
                new UserProfileFrame(user).setVisible(true); // 跳转到个人详情页
            }
        });

        placeOrderButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new OrderListFrame(user).setVisible(true); // 跳转到订单查看页
            }
        });

        productManagementButton.addActionListener(new ActionListener() { // 添加商品管理按钮事件监听器
            @Override
            public void actionPerformed(ActionEvent e) {
                new ProductManagementFrame(user).setVisible(true); // 跳转到商品管理页
            }
        });

        // 添加后台管理按钮事件监听器
        manageButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new AdminManagementFrame(user).setVisible(true); // 跳转到后台管理页
            }
        });

        add(panel);
    }
}